<template>
  <div class="css-boards">
    <div class="boards-item create" @click="onClickAction('boards-new')">
      <div>新建</div>
    </div>
    <div
      class="boards-item"
      v-for="(item, index) in cssItemList"
      :key="index"
      @click="onClickAction('boards-items', item)"
    >
      <div>
        <div v-html="item.style"></div>
        <div :id="`t${item.uuid}`" v-html="item.html">{{ item.html }}</div>
      </div>
    </div>
  </div>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
import { Getter, Action, ApiState } from '@/store';
import { renderCss } from '@/utils';
import { apiCssItemList, apiCssItemAdd } from '@/http/api/css';

@Component
export default class CssBoards extends Vue {
  @Prop() private label!: string;

  @ApiState(apiCssItemList.config.name)
  private cssItemList!: [];

  @Watch('label', { immediate: true })
  private watchLabel(to: string) {
    apiCssItemList({
      params: {
        page: 1,
        size: 10,
        label: to,
      },
    });
  }

  private onClickAction(type: string, model: any) {
    switch (type) {
      case 'boards-items':
        this.$emit('click-boards-items', model.id);
        break;
      case 'boards-new':
        apiCssItemAdd({
          data: {
            label: this.label,
            html: '<div>Hello world!</div>',
            css: 'color: orange;',
            pic: '',
          },
        }).then(res => {
          if (res.data.success) {
            this.$emit('click-boards-items', res.data.data.id);
          }
        });
        break;
    }
  }
}
</script>
<style lang="less">
.css-boards {
  display: grid;
  grid-template-columns: repeat(auto-fill, 400px);
  grid-template-rows: repeat(9999, 300px);
  overflow: auto;
  > .boards-item {
    overflow: hidden;
    padding: 20px;
    > div {
      background-color: white;
      height: 100%;
      cursor: pointer;
      overflow: auto;
    }
    &.create {
      > div {
        font-size: 36px;
        color: orangered;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>